package snake;

import boogie.game.Game;
import boogie.game.GameState;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.util.LinkedList;
import snake.controll.SnakeController;
import snake.snakeSprites.BorderBlock;
import snake.snakeSprites.Snake;

/**
 *
 * @author Boogie
 */
public final class PlayState extends GameState {

    public static final String NAME = "playState";
    private LinkedList<BorderBlock> border;
    private Snake snake;
    private int score = 0;
    private int growTimer = 0;

    public PlayState(Game game) {
        super(NAME, game);
        this.setSnakeSpeed(5);
    }

    @Override
    public void onInit() {
        snake = new Snake(this);
        SnakeController controller = new SnakeController(snake);
        this.getGame().addKeyListener(controller);

        border = new LinkedList<>();
        int N = 16;
        for (int i = 1; i <= N * 4; i++) {
            BorderBlock b = new BorderBlock(this);
            if (i > 0 && i <= N) {
                int x = 0;
                int y = (i - 1) * 32;
                b.setPosition(x, y);
            } else if (i > N && i <= N * 2) {
                int x = this.getGame().width - 32;
                int y = (i - 1 - N) * 32;
                b.setPosition(x, y);
            } else if (i > N * 2 && i <= N * 3) {
                int x = (i - 1 - N * 2) * 32;
                int y = 0;
                b.setPosition(x, y);
            } else if (i > N * 3 && i <= N * 4) {
                int x = (i - 1 - N * 3) * 32;
                int y = this.getGame().width - 32;
                b.setPosition(x, y);
            }
            border.add(b);
        }
    }

    @Override
    public void onUpdate() {
        growTimer += 1000 / this.getFpsRate();
        super.onUpdate();
        if (growTimer > 5000) {
            snake.grow();
            score += 10;
            growTimer = 0;
        }
    }

    public void setSnakeSpeed(int snakeSpeed) {
        this.setFpsRate(snakeSpeed);
    }

    @Override
    public void drawBackgorund(Graphics2D g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, this.getGame().width, this.getGame().height);
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.drawString("Score:" + score, 10, this.getGame().height - 10);
    }
}
